package com.tanhua.server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.QuestionMapper;
import com.tanhua.common.pojo.Question;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class QuestionService {

    @Autowired
    private QuestionMapper questionMapper;


    public Question queryQuestion(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        return this.questionMapper.selectOne(queryWrapper);
    }

    public void saveQuestion(Long userId, String content) {
        QueryWrapper<Question> wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId);
        Question question = questionMapper.selectOne(wrapper);
        if (ObjectUtil.isNotEmpty(question)) {
            //如果有问题就更改
            question.setTxt(content);
            questionMapper.updateById(question);
        } else {
            //如果没有就新增
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            question.setCreated(new Date());
            question.setUpdated(question.getCreated());
            questionMapper.insert(question);
        }
    }
}